package com.glory.good_dark.activity;

import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.adapter.MaterielListAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.MaterielEntity;
import com.glory.good_dark.entity.MaterielModelEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import butterknife.BindView;

/**
 * FileName: ReplaceMaterielActivity
 * Author: XJH
 * Date: 2020/3/19 17:11
 * Description: 更换物料
 * History:
 */
public class ReplaceMaterielActivity extends BaseProjectActivity {

    @BindView(R.id.rvMaterielList)
    RecyclerView rvMaterielList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private MaterielListAdapter materielListAdapter;
    private FreeUI_GeneralFragmentDialog dialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_replace_materiel;
    }

    @Override
    protected void initView() {
        initRecyclerView(rvMaterielList,this);
        materielListAdapter = new MaterielListAdapter(null,true);
        materielListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        materielListAdapter.isFirstOnly(true);
        rvMaterielList.setAdapter(materielListAdapter);

        materielListAdapter.setOnReplaceClickListener((v, s) -> {
            dialog = FreeApi_DialogUtils.getScanDialog(ReplaceMaterielActivity.this, "请输入物料条码", materiel -> {
                requestMateriel(s,materiel);
                dialog.dismiss();
            });
            dialog.show(getSupportFragmentManager(), "END_DIALOG");
        });
    }

    private void requestMateriel(MaterielModelEntity item,String materiel) {
//        showToast("materielType" + materielType + "materiel" + materiel,true);
//        initData();
        FreeUI_EntityCallBack<BaseEntity<MaterielModelEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<MaterielModelEntity>>(new TypeToken<BaseEntity<MaterielModelEntity>>() {
        }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<MaterielModelEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    showToast("更换物料成功",true);
                    initData();
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<MaterielModelEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAEQPMTMMATChange");
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        jsonObject.addProperty("MATERIAL_ID",materiel);
        jsonObject.addProperty("MATERIAL_TYPE",item.getMATERIAL_TYPE());
        jsonObject.addProperty("MAT_NO",item.getMAT_NO());
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAEQPMTMMATChange", jsonObject);
    }

    @Override
    protected void initData() {
//        String ss = "{\n" +
//                "    \"data\":{\n" +
//                "        \"HEADER\":{\n" +
//                "            \"MACHINENAME\":\"PDA-MT9001\",\n" +
//                "            \"MESSAGENAME\":\"PDAEQPMTMMATInfo\",\n" +
//                "            \"TRANSACTIONID\":\"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "            \"EVENTTIME\":\"2019-03-16 16:28:59.585\"\n" +
//                "        },\n" +
//                "        \"BODY\":{\n" +
//                "            \"EQP_ID\":\"LC128\",\n" +
//                "            \"MTM_MAT_LIST\":[\n" +
//                "                {\n" +
//                "                    \"RUN_ID\":102,\n" +
//                "                    \"EQP_ID\":\"LC128\",\n" +
//                "                    \"DESCRIPTION\":\"TS00001\",\n" +
//                "                    \"MATERIAL_ID\":\"T1\",\n" +
//                "                    \"MATERIAL_TYPE\":\"4KUAISU1\",\n" +
//                "                    \"MATERIAL_NAME\":\"晶粒\",\n" +
//                "                    \"ONEQP_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "                    \"OFFEQP_TIME\":\"2019-03-16 19:28:59\",\n" +
//                "                    \"EXPIRATION_TIME\":\"2029-03-16 16:28:59\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"RUN_ID\":102,\n" +
//                "                    \"EQP_ID\":\"LC128\",\n" +
//                "                    \"DESCRIPTION\":\"TS00001\",\n" +
//                "                    \"MATERIAL_ID\":\"T2\",\n" +
//                "                    \"MATERIAL_TYPE\":\"4KUAISU2\",\n" +
//                "                    \"MATERIAL_NAME\":\"焊条\",\n" +
//                "                    \"ONEQP_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "                    \"OFFEQP_TIME\":\"2019-03-16 19:28:59\",\n" +
//                "                    \"EXPIRATION_TIME\":\"2029-03-16 16:28:59\"\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"RUN_ID\":102,\n" +
//                "                    \"EQP_ID\":\"LC128\",\n" +
//                "                    \"DESCRIPTION\":\"TS00001\",\n" +
//                "                    \"MATERIAL_ID\":\"T3\",\n" +
//                "                    \"MATERIAL_TYPE\":\"4KUAISU3\",\n" +
//                "                    \"MATERIAL_NAME\":\"环氧\",\n" +
//                "                    \"ONEQP_TIME\":\"2019-03-16 16:28:59\",\n" +
//                "                    \"OFFEQP_TIME\":\"2019-03-16 19:28:59\",\n" +
//                "                    \"EXPIRATION_TIME\":\"2029-03-16 16:28:59\"\n" +
//                "                }\n" +
//                "            ]\n" +
//                "        },\n" +
//                "        \"Return\":{\n" +
//                "            \"RETURNCODE\":\"0\",\n" +
//                "            \"RETURNMESSAGE\":\"\"\n" +
//                "        }\n" +
//                "    }\n" +
//                "}";
//        BaseEntity<MaterielEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<MaterielEntity>>() {}.getType());
//        materielListAdapter.setNewData(bean.getData().getBody().getMTM_MAT_LIST());
        FreeUI_EntityCallBack<BaseEntity<MaterielEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<MaterielEntity>>(new TypeToken<BaseEntity<MaterielEntity>>() {
        }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<MaterielEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    materielListAdapter.setNewData(response.body().getData().getBody().getMTM_MAT_LIST());
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<MaterielEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAEQPMTMMATInfo");
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAEQPMTMMATInfo", jsonObject);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设备上已使用的物料");
        freeUI_titleView.setRightListener("伸缩", v -> {
            rvMaterielList.setVisibility(rvMaterielList.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {

    }
}
